package com.china08.yxyapi.api.manage.tec.teaching.continuedu.teatrainlesson;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLesson;
import com.china08.yxyapi.entity.mg.continuedu.YxyContinueduUserFavorite;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonRepository;
import com.china08.yxyapi.repository.mg.continuedu.YxyContinueduUserFavoriteRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 
 * @annotation: 继续教育-师训课程-收藏课程
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class YxyTeaTrainLessonCollectController extends YxyApiBaseController {

    @Autowired
    private YxyContinueduLessonRepository yxyContinueduLessonRepository;

    @Autowired
    private YxyContinueduUserFavoriteRepository yxyContinueduUserFavoriteRepository;

    @Autowired
    private RestTemplate restTemplate;

    @Value(value = "${yx.rest.api.yx.path}")
    private String yxyApiYxUrl;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/continuedu/teatrainlesson/collect/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String userId = getProfile(request, response).getId();

        YxyContinueduLesson lesson = yxyContinueduLessonRepository.findById(reqModel.lessonId);
        String id = userId + "_" + reqModel.lessonId;
        YxyContinueduUserFavorite favorite = new YxyContinueduUserFavorite();
        favorite.setCount(lesson.getCount());
        favorite.setId(id);
        favorite.setLessonId(reqModel.lessonId);
        favorite.setLessonName(lesson.getName());
        favorite.setUploadDate(DateToolUtils.format(lesson.getUpDate(), DateToolUtils.FORMAT_SHORT_YMD));
        favorite.setUploadUserId(lesson.getCreatedBy());
        favorite.setCover(lesson.getCover());
        // Map<String, String> map = restTemplate.getForObject(yxyApiYxUrl +
        // "/comm/user/username" + lesson.getUserId(),
        // Map.class);
        favorite.setUploadUserName(lesson.getAuthor());
        favorite.setUserId(userId);
        yxyContinueduUserFavoriteRepository.save(favorite);
        RespModel respModel = new RespModel();
        respModel.setMessage("收藏成功");

        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 课程Id
        public String lessonId;

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
